package com.portal.framework.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
/**
 * dom4j工具类
 * @author 卢烁波
 * @date 2007-3-10
 *
 */
public class Dom4jUtil {
	/**
	 * 写文件
	 * 
	 * @param document
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void writerFile(Document document,String filePath) throws IOException,
			FileNotFoundException {
		XMLWriter output = null;
		try {
			OutputFormat format = OutputFormat.createPrettyPrint();
			format.setEncoding("UTF-8");
			output = new XMLWriter(new FileOutputStream(filePath), format);
			output.write(document);
		} finally {
			try {
				output.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 解析文件
	 * 
	 * @return
	 * @throws FileNotFoundException
	 * @throws DocumentException
	 */
	public static Document parse(String filePath) throws FileNotFoundException, DocumentException {
		FileInputStream fileInputStream = new FileInputStream(filePath);
		SAXReader sax = new SAXReader();
		Document document = sax.read(fileInputStream);
		return document;
	}
}
